﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using WebTree.ZTree;

namespace WebZb.TestPage.TreeView
{
    /// <summary>
    /// getdata 的摘要说明
    /// </summary>
    public class zgetdata : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {

            context.Response.ContentType = "text/plain";
            string res = string.Empty;
            SqlParameter[] parms;

            string action = context.Request.QueryString["action"] ?? "";
            string nodeId = context.Request.Params["id"] ?? "";
            string newName = context.Request.Params["name"] ?? "";
            string mPreId = context.Request.Params["pId"] ?? "";

            switch (action)
            {
                case "getdata":

                    res = ShowData(context);
                    context.Response.Write(res);
                    break;

                case "add":
                    parms = new SqlParameter[] {                                      
                        new SqlParameter("@mPreId",mPreId),
                        new SqlParameter("@mName",newName)
                    };
                    SqlHelper.Execute("INSERT INTO MenuTab (mPreId ,mName ) VALUES (@mPreId ,@mName )", parms);

                    res = ShowData(context);
                    context.Response.Write(res);

                    break;

                case "rename":
                    parms = new SqlParameter[] {
                        new SqlParameter("@id",nodeId),
                        new SqlParameter("@mName",newName)
                    };
                    SqlHelper.Execute("update MenuTab set mName=@mName where mid=@id", parms);

                    res = ShowData(context);
                    context.Response.Write(res);

                    break;
                case "remove":
                    parms = new SqlParameter[] {
                        new SqlParameter("@id",nodeId),                        
                    };
                    SqlHelper.Execute("delete MenuTab  where mid=@id", parms);
                    res = ShowData(context);
                    context.Response.Write(res);
                    break;
                case "move":

                    

                    var nodeList = nodeId.TrimEnd(',').Split(',');

                    foreach (var node in nodeList)
                    {
                        parms = new SqlParameter[] 
                        {
                            new SqlParameter("@id",node),                                          
                            new SqlParameter("@mPreId",mPreId),  
                        };
                        SqlHelper.Execute("update MenuTab set mPreId=@mPreId where mid=@id", parms);
                    }



                    res = ShowData(context);
                    context.Response.Write(res);

                    break;

                case "":
                    break;
                default:
                    break;
            }





        }

        private static string ShowData(HttpContext context)
        {
            var dtTmp = SqlHelper.Query("SELECT [mId] as id  ,[mPreId] as pid ,[mName] as name  FROM [MenuTab]", null);

            var list = SqlHelper.ToEntities<MenuEntity>(dtTmp);
            var res = Newtonsoft.Json.JsonConvert.SerializeObject(list);
            return res;
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}